/**
 * 
 */
/***************************************************************
                           用户管理模块
***************************************************************/
var addDocDialog;
function docManagerInit()
{
	 $("#docTable tr:nth-child(even)").addClass("trevencolor");
	 BUI.use(['bui/overlay','bui/form'],function(Overlay,Form){
	      	var form = new Form.HForm({
	        	srcNode : '#form'
   	 }).render();
	 
	 addDocDialog = new Overlay.Dialog({
         title:'添加用户',
         width:500,
         height:320,
         //配置DOM容器的编号
         contentId:'doc_content',
         cancel:function(){
	      	choose_close();
	      	clearDocDialog();
	     },
         success:function () {
        	 
        	saveOrUpdateDoc();
        	
           }
       });
   });
	 
   $("#btnShow").bind("click",function(){
	   clearDocDialog();
	   addDocDialog.set("title","添加用户");
	   $("#type").val("1");
	   addDocDialog.show();
	   choose_open();
   });
   
   docValidate();
}

/**
 *	添加用户 type=1
 *  编辑用户 type=2
 */
function saveOrUpdateDoc()
{
	var type = $("#type").val();
	var name =$("#name").val();
//	findDocByName(name);
	
	if($("#docForm").validate().form()){
		addDocDialog.close();
		var url = projectName + "/doc/saveOrUpdate.do?date=" + new Date().getTime();
		$.ajax({
			url: url,
			type: "post",
			async: false,
			data: $('#docForm').serialize(),
			success: function(data,result)
			{
				if(type == 1)
				{
					if("SUCCESS" == data)
					{
						messageAlert(1,"添加用户成功",function()
						{
							this.close();
							choose_close();
							showDocPageDataList(1);
//							var DocId = getDocId();
//							saveLog(DocId,1,1,2,DocName);
						});
					}
					else
					{
						messageAlert(1,"添加用户失败，请重试",function()
								{
							this.close();
							choose_close();
							showDocPageDataList(1);
						});
					}
				}
				else
				{
					if("SUCCESS" == data){
						messageAlert(1,"修改用户成功",function()
						{
							this.close();
							choose_close();
							showDocPageDataList(1);
//							var DocId = getDocId();
//							saveLog(DocId,1,1,3,DocName);
						});
					}
					else
					{
						messageAlert(1,"修改用户失败，请重试",function()
								{
							this.close();
							choose_close();
							showDocPageDataList(1);
						});
					}
					
				}
				
			}
		});
	}
}

/**
 *	清空表单数据 
 */
function clearDocDialog(){
	docFormValidate.resetForm();
	$("#eno").val("");
	$("#eno").attr("readonly",false);
	$("#eno").css("background-color","");
	$("#name").val("");
	$("#name").attr("readonly",false);
	$("#name").css("background-color","");
	$("#password").val("");
	$("#passwordTwo").val("");
	$("#pmsn").val("普通权限");
}

/**
 *	冻结、解冻用户
 *
 */
function freeze(id,status){
	var url;
	if(status == 1)
	{
		url = projectName + "/doc/unFreeByIds.do?date=" + new Date().getTime();
	}
	else
	{
		url = projectName + "/doc/freeByIds.do?date=" + new Date().getTime();
	}
	$.ajax({
		 url: url,
		 type: "post",
		 data: {"ids": id, "status": status},
		 async: false,
		 success: function(data,result){
			 if(status == 1)
			 {
				 if("SUCCESS" == data)
				 {
					 messageAlert(1,"用户冻结成功",function()
					 {
							this.close();
							choose_close();
							showDocPageDataList(1);
//							var DocId = getDocId();
//							var mgrEntity = getDocNames(id);
//							saveLog(DocId,1,1,5,mgrEntity);
					 });
				 }
				 else if("ERROR" == data)
				 {
					 messageAlert(3,"用户冻结失败",function(){
							this.close();
							choose_close();
							showDocPageDataList(1);
					 });
				 }
			 }
			 else
			 {
				 if("SUCCESS" == data)
				 {
					 messageAlert(1,"用户解冻成功",function()
					 {
							this.close();
							choose_close();
							showDocPageDataList(1);
//							var DocId = getDocId();
//							var mgrEntity = getDocNames(id);
//							saveLog(DocId,1,1,6,mgrEntity);
					 });
				 }
				 else if("ERROR" == data)
				 {
					 messageAlert(3,"用户解冻失败",function(){
							this.close();
							choose_close();
							showDocPageDataList(1);
					 });
				 }
			 }
			 
		 }
	 });
}

/**
 *	判断批量冻结用户是否为空 
 */
function unfree_doc(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要冻结的用户！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要冻结选择的用户吗？',function(){choose_open,unFreeByIds(ids)},1)
	}
}

/**
 *	批量冻结用户 
 */
function unFreeByIds(ids){
	$.ajax({
		 url: projectName+"/doc/unFreeByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids":ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"用户冻结成功",function()
				 {
						this.close();
						choose_close();
						showDocPageDataList(1);
//						var DocId = getDocId();
//						var mgrEntity = getDocNames(ids);
//						saveLog(DocId,1,1,5,mgrEntity);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"用户冻结失败",function(){
						this.close();
						choose_close();
						showDocPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	根据Ids获取用户名
 */
function getDocNames(ids)
{
	var mgrEntity = "";
	$.ajax({
		url: projectName+"/doc/getNames.do?date=" + new Date().getTime(),
		type: "post",
		data: {"ids": ids},
		async: false,
		success: function(data)
		 {
			 mgrEntity = data;
		 }
	});
	
	return mgrEntity;
}

/**
 *	删除用户 
 */
function deleteDoc(id)
{
//	var mgrEntity = getDocNames(id);
	$.ajax({
		 url: projectName+"/doc/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": id},
		 async: false,
		 success: function(data,result)
		 {
			 if("SUCCESS" == data){
				 messageAlert(1,"删除用户成功",function()
				 {
						this.close();
						choose_close();
						showDocPageDataList(1);
//						var DocId = getDocId();
//						saveLog(DocId,1,1,4,mgrEntity);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除用户失败",function(){
						this.close();
						choose_close();
						showDocPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	判断批量删除用户是否为空
 */
function del_doc(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要删除的用户！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要删除选择的用户吗？',function(){choose_open,deleteByIds(ids)},1)
	}
}

/**
 *	批量删除用户 
 */
function deleteByIds(ids){
//	var mgrEntity = getDocNames(ids);
	$.ajax({
		 url : projectName + "/doc/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"删除用户成功",function(){
						this.close();
						choose_close();
						showDocPageDataList(1);
//						var DocId = getDocId();
//						saveLog(DocId,1,1,4,mgrEntity);
				 });
			 }
			 
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除用户失败",function(){
						this.close();
						choose_close();
						showDocPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	编辑用户 
 */
function docEdit(id)
{
	$.ajax({
		url : projectName+"/doc/findDocById.do?date=" + new Date().getTime(),
		async : false,
		data:{"id":id},
		success : function(data) {
			if(null != data&&data != "")
			{
				clearDocDialog();
				$("#eno").val(data.eno);
				$("#eno").attr("readonly","readonly");
				$("#eno").css("background-color","#F0F0F0");
				$("#name").val(data.name);
				$("#name").attr("readonly","readonly");
				$("#name").css("background-color","#F0F0F0");
				$("#type").val("2");
				if(data.pmsn == "1")
				{
					$("#pmsn").val("管理员权限");
				}
				else if(data.pmsn == "2")
				{
					$("#pmsn").val("操作员权限");
				}
				else
				{
					$("#pmsn").val("普通");
				}
				choose_open();
				addDocDialog.set("title","编辑用户");
				addDocDialog.show();		
			}
			else
			{
				messageAlert(0,"该用户已被删除！",function(){
					choose_close(),
					this.close(),
					showDocPageDataList(1)
					});
			}
		}
	});
	
}


/***************************************************************
							用户分页
***************************************************************/
/**
 *	用户分页对象 
 */
var docPageInfo={
		orderBy:null,
		order:"desc",
		pageNo:"1",
};

/**
 * 获取用户名查询条件
 */
function getUserName()
{
	var userName =  $("#user_Name").val();
	if(undefined == userName||null == userName)
	{
		userName = "";
	}
	return userName;
}


/**
 * 查询用户数据
 */
function showDocPageDataList(pageNo)
{
	docPageInfo.pageNo = pageNo;
	var userName = getUserName();
	var docPageInfoString = JSON.stringify(docPageInfo);
	window.location.href = projectName + "/doc/getPageDoc.do?PageInfo=" + docPageInfoString + "&userName=" + userName;
}

/***************************************************************
							表单验证
***************************************************************/
var docFormValidate;
function docValidate()
{
	docFormValidate = $("#docForm").validate({
		errorPlacement: function(error, element) { 
         	error.appendTo(element.next());
         },
		rules:{
			eno:
			{
				required:true,
				theTrueEno:true
			},
			name: 
			{
				required:true,
				maxlength:20,
				theSameName:true
			},
			password:
			{
				required:function(){
					var pwd1 = $("#password").val();
					var type = $("#type").val();
					if(type == 2)
					{
						return false;
					}
					else if(pwd1 == "")
					{
						return true;
					}
					else
					{
						return false;
					}
						
				},
			},
			passWordTwo:
			{
				required:function(){
					var pwd2 = $("#passWordTwo").val();
					var type = $("#type").val();
					if(type == 2)
					{
						return false;
					}
					else if(pwd2 == "")
					{
						return true;
					}
					else
					{
						return false;
					}
				},
				equalTo:"#password"
			}
		},
		messages:{
			name:
			{
                required:"用户名不能为空"
            },
            password:
            {
            	required:"密码不能为空"
            },
            passWordTwo:
            {
                required:"确认密码不能为空",
                equalTo:"2次密码输入不一致"
            }                                
        }
		
	});

}

/**
 *	验证是否重名
 */
function findDocByName(name){
	var flag = true;
	$.ajax({
		url: projectName + "/doc/findDocByName.do?date=" + new Date().getTime(),
		async:false,
		data:{"name":name},
		type:"post",
		success:function(result){
			if("SUCCESS" == result)
			{
		        flag = false;
			}
			else if("ERROR" == result)
			{
				flag = true; 
			}
			else
			{
				flag = false; 
			}
			
		},
	});
	var type = $("#type").val();
	if(type == 2)
	{
		flag = true;
	}
	return flag ;
}


	
